home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libiconv_src.lha / src / gbk.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-07  |  3.8 KB  |  154 lines

  1. /*
  2.  * GBK
  3.  */
  4.  
  5. /*
  6.  * GBK, as described in Ken Lunde's book, is an extension of GB 2312-1980
  7.  * (shifted by adding 0x8080 to the range 0xA1A1..0xFEFE, as used in EUC-CN).
  8.  * It adds the following ranges:
  9.  *
  10.  * (part of GBK/1)  0xA2A1-0xA2AA  Small Roman numerals
  11.  * GBK/3   0x{81-A0}{40-7E,80-FE}  6080 new characters, all in Unicode
  12.  * GBK/4   0x{AA-FE}{40-7E,80-A0}  8160 new characters, 8080 in Unicode
  13.  * GBK/5   0x{A8-A9}{40-7E,80-A0}  166 new characters, 153 in Unicode
  14.  */
  15.  
  16. /*
  17.  * CP936 is nearly identical to GBK. It differs as follows:
  18.  *
  19.  * 1. Some characters in the GB2312 range are defined differently:
  20.  *
  21.  *     code    GB2312                         CP936.TXT
  22.  *    0xA1A4   0x30FB # KATAKANA MIDDLE DOT   0x00B7 # MIDDLE DOT
  23.  *    0xA1AA   0x2015 # HORIZONTAL BAR        0x2014 # EM DASH
  24.  *
  25.  * 2. 19 characters added in the range 0xA6E0-0xA6F5.
  26.  *
  27.  * 3. 4 characters added in the range 0xA8BB-0xA8C0.
  28.  */
  29.  
  30. /*
  31.  * Since all three tables I have looked at
  32.  *   - the CP936 table by Microsoft, found on ftp.unicode.org,
  33.  *   - the GBK table by Sun, investigated on a Solaris 2.7 machine,
  34.  *   - the GBK tables by CWEX, found in the Big5+ package,
  35.  * all include these CP936 extensions (the CWEX tables have additional
  36.  * differences), I conclude that either Ken Lunde has overlooked some of
  37.  * the differences between GB2312 and GBK, or he is right but the major
  38.  * vendors don't care about it. In either case, CP936 is the de facto
  39.  * standard under the name "GBK", and we should better support it.
  40.  *
  41.  * So in what follows, when we write "GBK" we always mean "CP936".
  42.  */
  43.  
  44. #include "gbkext1.h"
  45. #include "gbkext2.h"
  46. #include "gbkext_inv.h"
  47. #include "cp936ext.h"
  48.  
  49. static int
  50. gbk_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  51. {
  52.   unsigned char c = *s;
  53.  
  54.   if (c >= 0x81 && c < 0xff) {
  55.     if (n < 2)
  56.       return RET_TOOFEW(0);
  57.     if (c >= 0xa1 && c <= 0xf7) {
  58.       unsigned char c2 = s[1];
  59.       if (c == 0xa1) {
  60.         if (c2 == 0xa4) {
  61.           *pwc = 0x00b7;
  62.           return 2;
  63.         }
  64.         if (c2 == 0xaa) {
  65.           *pwc = 0x2014;
  66.           return 2;
  67.         }
  68.       }
  69.       if (c2 >= 0xa1 && c2 < 0xff) {
  70.         unsigned char buf[2];
  71.         int ret;
  72.         buf[0] = c-0x80; buf[1] = c2-0x80;
  73.         ret = gb2312_mbtowc(conv,pwc,buf,2);
  74.         if (ret != RET_ILSEQ)
  75.           return ret;
  76.         buf[0] = c; buf[1] = c2;
  77.         ret = cp936ext_mbtowc(conv,pwc,buf,2);
  78.         if (ret != RET_ILSEQ)
  79.           return ret;
  80.       }
  81.     }
  82.     if (c >= 0x81 && c <= 0xa0)
  83.       return gbkext1_mbtowc(conv,pwc,s,2);
  84.     if (c >= 0xa8 && c <= 0xfe)
  85.       return gbkext2_mbtowc(conv,pwc,s,2);
  86.     if (c == 0xa2) {
  87.       unsigned char c2 = s[1];
  88.       if (c2 >= 0xa1 && c2 <= 0xaa) {
  89.         *pwc = 0x2170+(c2-0xa1);
  90.         return 2;
  91.       }
  92.     }
  93.   }
  94.   return RET_ILSEQ;
  95. }
  96.  
  97. static int
  98. gbk_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  99. {
  100.   unsigned char buf[2];
  101.   int ret;
  102.  
  103.   if (wc != 0x30fb && wc != 0x2015) {
  104.     ret = gb2312_wctomb(conv,buf,wc,2);
  105.     if (ret != RET_ILSEQ) {
  106.       if (ret != 2) abort();
  107.       if (n < 2)
  108.         return RET_TOOSMALL;
  109.       r[0] = buf[0]+0x80;
  110.       r[1] = buf[1]+0x80;
  111.       return 2;
  112.     }
  113.   }
  114.   ret = gbkext_inv_wctomb(conv,buf,wc,2);
  115.   if (ret != RET_ILSEQ) {
  116.     if (ret != 2) abort();
  117.     if (n < 2)
  118.       return RET_TOOSMALL;
  119.     r[0] = buf[0];
  120.     r[1] = buf[1];
  121.     return 2;
  122.   }
  123.   if (wc >= 0x2170 && wc <= 0x2179) {
  124.     r[0] = 0xa2;
  125.     r[1] = 0xa1 + (wc-0x2170);
  126.     return 2;
  127.   }
  128.   ret = cp936ext_wctomb(conv,buf,wc,2);
  129.   if (ret != RET_ILSEQ) {
  130.     if (ret != 2) abort();
  131.     if (n < 2)
  132.       return RET_TOOSMALL;
  133.     r[0] = buf[0];
  134.     r[1] = buf[1];
  135.     return 2;
  136.   }
  137.   if (wc == 0x00b7) {
  138.     if (n < 2)
  139.       return RET_TOOSMALL;
  140.     r[0] = 0xa1;
  141.     r[1] = 0xa4;
  142.     return 2;
  143.   }
  144.   if (wc == 0x2014) {
  145.     if (n < 2)
  146.       return RET_TOOSMALL;
  147.     r[0] = 0xa1;
  148.     r[1] = 0xaa;
  149.     return 2;
  150.   }
  151.  
  152.   return RET_ILSEQ;
  153. }
  154.